package com.info.server;

import org.apache.log4j.Logger;
import org.net.operation.TerServer;

import com.info.threads.GetPutInfoThread;
import com.info.threads.PutInfoThread;
import com.info.threads.ScanThread;
import com.info.threads.TerPoolThread;
import com.info.utils.ConfigManager;

/**
 * @author HAHA
 * 
 */
public class StartServer {
	
	private static Logger logger=Logger.getLogger(StartServer.class);
	public ConfigManager cfg = null;
	public int terPort = 0;
	public int appPort = 0;
	public static String gate_code = "";
	public static String normal_code = "";
	public static int SEND_INTERVAL = 5;
	public static int SEND_BUF = 2048;
	public static int GATE_RECV_BUFFER = 2048;

	public static String MSG_START_FLAG = "5468";
	public static String MSG_END_FLAG = "0D0A";
	public static String UP_TEMINAL_ONLINE = "Y";
	public static String UP_TERMINAL_DOWNLINE = "N";
	public static String Heart_Beat_402 = "42";
	public static String GPS_POSITIONING_402 = "41";
	public static String GPS_DATA_INTERVAL_402 = "43";
	public static String GPS_LOCATION_802 = "81";
	public static String BASE_STATION_802 = "82";
	public static String TARGET_LOCATION_802 = "83";
	public static String HEART_RATE_802 = "84";
	public static String ECG_802 = "85";
	public static String PEDOMETER_802 = "86";
	public static String ONE_BYTE = "00";
	public static String ISuCCESS="01" ;
	public static String ISFAIL="00";
	public static String DEBUG="1";
	public static int conSum = 1500;

	public static long overTime = 180000L;  //注册终端3分钟不更新关闭连接
	public static long pollquit = 90000L;  //15分钟中断所有线程
	public static long timequit = 300000L; //50分钟重启启动服务
	public static int terTimeQuit = 20000; //非法终端20秒关闭连接
	public static long gameOver=86400000L; //24小时刷新终端信息缓存
	public static long scanPutInfo=30000; //30秒获取下发数据
	public static String logFile = "config.properties";
	public static boolean logFlag = true;

	public static TerPoolThread terPool = null;
	private TerServer terServer = null;
	private GetPutInfoThread infoThread=null;
	private PutInfoThread putInfoThread=null;
	
	private void initProperties() {
		this.cfg = new ConfigManager();
		this.cfg.read("config.properties");
	}

	public int toInt(String str) {
		return Integer.parseInt(str);
	}

	/**
	 * 初始化基础信息
	 */
	private void initSocketInfo() {
		this.terPort = toInt(this.cfg.getValue("TerPort"));

		gate_code = this.cfg.getValue("GATE_CODE");
		normal_code = this.cfg.getValue("NORMAL_CODE");
		DEBUG=this.cfg.getValue("DEBUG");
		SEND_INTERVAL = toInt(this.cfg.getValue("SEND_INTERVAL"));
		SEND_BUF = toInt(this.cfg.getValue("SEND_BUFFER"));
		GATE_RECV_BUFFER = toInt(this.cfg.getValue("REC_GATE_BUFFER"));
		timequit = toInt(this.cfg.getValue("TIME_QUIT_INTERVAL")) * 1000;
		terTimeQuit = toInt(this.cfg.getValue("TER_QUIT_INTERVAL")) * 1000;
		overTime = toInt(this.cfg.getValue("OVER_QUIT_INTERVAL")) * 1000;
		pollquit = toInt(this.cfg.getValue("POLL_QUIT_INTERVAL")) * 1000;
		gameOver = toInt(this.cfg.getValue("REFRESH_MEMORY_INTERVAL")) * 1000;
		scanPutInfo = toInt(this.cfg.getValue("REFRESH_PUTINFO_INTERVAL")) * 1000;
		pollquit = 20000L;
	}

	/**
	 * 初始化处理池
	 */
	public void initTerThreadPool() {
		int num = toInt(this.cfg.getValue("TerThreadNum"));
		terPool = new TerPoolThread(num);
	}
	
	/**
	 * 初始化获取下发信息
	 */
	public void initGetPutInfoThread() {
		infoThread=new GetPutInfoThread();
		infoThread.start();
	}
	
	/**
	 * 初始化下发
	 */
	public void initPutInfoThread() {
		putInfoThread=new PutInfoThread();
		putInfoThread.start();
	}

	/**
	 * 初始化网络连接
	 * @throws Exception
	 */
	public void initTerminalSocket() throws Exception {
		this.terServer = new TerServer(this.terPort, "", gate_code, terPool);
		Thread th = new Thread(this.terServer);
		th.start();

		ScanThread poll = new ScanThread(this.terServer);
		poll.start();
		logger.info("SHW#启动轮询处理终端长期离线线程");
	}

	public static void main(String args[]) {
		StartServer gate = new StartServer();
		try {
			gate.initProperties();
			gate.initSocketInfo();
			logger.info("SHW#初始化系统配置");
			gate.initTerThreadPool();
			logger.info("SHW#初始化信息处理线程池成功");
			gate.initTerminalSocket();
			gate.initGetPutInfoThread();
			gate.initPutInfoThread();
			logger.info("SHW#初始化终端服务器成功");
			logger.info("SHW#初始化应用程序服务器成功");
			logger.info("SHW#被激活的线程" + Thread.activeCount());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}